import { _decorator, color, Component, ImageAsset, Label, Node, Sprite, SpriteFrame, Texture2D, Tween, tween, v3 } from 'cc';
import { AudioEffect } from './AudioEffect';
import { RandomUtils } from './Utils';
const { ccclass, property } = _decorator;

@ccclass('main')
export class main extends Component {

    @property(AudioEffect)
    audioEffect:AudioEffect = null


    private _shoots:Node[] = []
    private _flower:Node[] = []
    start() {
        for (let index = 0; index < 10; index++) {
            const node = this.node.getChildByName("shoot" + (index + 1));
            node.active = false
            this._shoots.push(node)
        }
        for (let index = 0; index < 13; index++) {
            const node = this.node.getChildByName("flower_" + (index + 1));
            node.active = false
            this.delBlack(node)
            this._flower.push(node)
        }


        let lab = this.node.getChildByName("Label")
        let tmpColor = color(255,255,255,255)
        lab.getComponent(Label).color = tmpColor
        let val = 5
        tween(lab).repeatForever(
            tween(lab).call(()=>{
                tmpColor.a -= val
                if(tmpColor.a <= 100){
                    tmpColor.a = 100
                    val *= -1
                }
                if (tmpColor.a >= 255) {
                    tmpColor.a = 255
                    val *= -1
                }
                lab.getComponent(Label).color = tmpColor
            }).delay(0.02)
        ).start()
    }

    delBlack(node:Node){
        let sprite = node.getComponent(Sprite)
        if (sprite && sprite.spriteFrame) {
            const spriteFrame = sprite.spriteFrame;
            const texture = spriteFrame.texture as Texture2D;
            const image = texture.image as ImageAsset;

            if (image && image.data) {
                const width = image.width;
                const height = image.height;
                const canvas = document.createElement('canvas');
                const ctx = canvas.getContext('2d');

                canvas.width = width;
                canvas.height = height;
                ctx.drawImage(image.data as HTMLImageElement, 0, 0);

                const imageData = ctx.getImageData(0, 0, width, height);
                const data = imageData.data;

                for (let i = 0; i < data.length; i += 4) {
                    if (data[i] < 10 && data[i + 1] < 10 && data[i + 2] < 10) {
                        data[i + 3] = 0; // 将黑色部分的透明度设置为 0
                    }
                }

                ctx.putImageData(imageData, 0, 0);
                const newTexture = new Texture2D();
                const newImageAsset = new ImageAsset(canvas);

                newTexture.image = newImageAsset;
                const newSpriteFrame = new SpriteFrame();
                newSpriteFrame.texture = newTexture;

                sprite.spriteFrame = newSpriteFrame;
            }
        }
    }
    update(deltaTime: number) {
        if(RandomUtils.random(1,9999999)%100 > 90 && this._shoots.length > 0 && this._flower.length > 0){
           let shoot = this._shoots[0]
           this._shoots.splice(0,1)
           let x = RandomUtils.random(-600,600)
           let y =  RandomUtils.random(100,250)
           tween(shoot).call(()=>{
                shoot.setPosition(x, -400)
                shoot.active = true
                this.audioEffect.playShoot()
           }).to(0.5,{position:v3(x, y)})
           .call(()=>{
                this.audioEffect.playBomb()
                this._shoots.push(shoot)
                shoot.active = false
                let flower = this._flower[0]
                flower.active = true
                this._flower.splice(0, 1)
                flower.setPosition(x,y)
                let tmpColor = color(255,255,255,255)
                flower.getComponent(Sprite).color = tmpColor
                tween(flower).repeatForever(
                    tween(flower).call(()=>{
                        tmpColor.a -= 10
                        if(tmpColor.a <= 0){
                            tmpColor.a = 0
                            flower.active = false
                            flower.getComponent(Sprite).color = tmpColor
                            Tween.stopAllByTarget(flower)
                            this._flower.push(flower)
                            return
                        }
                        flower.getComponent(Sprite).color = tmpColor
                    }).delay(0.02)
                ).start()
           }).start()
        }
    }
}


